<script>
let WebIM = uni.WebIM = require("utils/WebIM")["default"];
let getMsgData = require("utils/getMsgData");
// let msgStorage = require("comps/chat/msgstorage");
let msgType = require("utils/msgtype");
// let ToastPannel = require("./comps/toast/toast");
let disp = require("utils/broadcast");
let logout = false;

//emedia_for_miniProgram-test是沙箱环境测试版，线上环境请用emedia_for_miniProgram这个文件
// const emedia = uni.emedia = require("./emedia/emedia_for_miniProgram") 

// console.log('emedia', emedia)
console.log('WebIM', WebIM)

function ack(receiveMsg){
	// 处理未读消息回执
	var bodyId = receiveMsg.id;         // 需要发送已读回执的消息id
	var ackMsg = new WebIM.message("read", WebIM.conn.getUniqueId());
	ackMsg.set({
		id: bodyId,
		to: receiveMsg.from
	});
	WebIM.conn.send(ackMsg.body);
}

function onMessageError(err){
	if(err.type === "error"){
		uni.showToast({
			title: err.errorText
		});
		return false;
	}
	return true;
}

function getCurrentRoute(){
	let pages = getCurrentPages();
	let currentPage = pages[pages.length - 1];
	return currentPage.route;
}

function calcUnReadSpot(message){
	let myName = uni.getStorageSync("myUsername");
	let members = uni.getStorageSync("member") || []; //好友
	var listGroups = uni.getStorageSync('listGroup')|| []; //群组
	let allMembers = members.concat(listGroups)
	let count = allMembers.reduce(function(result, curMember, idx){
		let chatMsgs;
		if (curMember.groupid) {
			chatMsgs = uni.getStorageSync(curMember.groupid + myName.toLowerCase()) || [];
		}else{
			chatMsgs = uni.getStorageSync(curMember.name.toLowerCase() + myName.toLowerCase()) || [];
		}
		return result + chatMsgs.length;
	}, 0);
	getApp().globalData.unReadMessageNum = count;
	disp.fire("em.xmpp.unreadspot", message);
}



	export default {
		globalData: {
			unReadMessageNum: 0,
			userInfo: null,
			saveFriendList: [],
			saveGroupInvitedList: [],
			isIPX: false ,//是否为iphone X
			closed: false,
			curOpenOpt: {},
			username:''
		},
		onLaunch: function() {
			// 调用 API 从本地缓存中获取数据
			uni.setInnerAudioOption({obeyMuteSwitch: false})
			var me = this;
			var logs = uni.getStorageSync("logs") || [];
			logs.unshift(Date.now());
			uni.setStorageSync("logs", logs);
			
			// 
			disp.on("em.main.ready", function(){
				calcUnReadSpot();
			});
			disp.on("em.chatroom.leave", function(){
				calcUnReadSpot();
			});
			disp.on("em.chat.session.remove", function(){
				calcUnReadSpot();
			});
			disp.on('em.chat.audio.fileLoaded', function(){
				calcUnReadSpot()
			});
			
			disp.on('em.main.deleteFriend', function(){
				calcUnReadSpot()
			});
			disp.on('em.chat.audio.fileLoaded', function(){
				calcUnReadSpot()
			});
			
			// 
			WebIM.conn.listen({
				onOpened(message){
					console.log('im登录成功')
					uni.navigateTo({
						url:'/pages/index/index'
					})
					// WebIM.conn.setPresence();
					if(getCurrentRoute() == "pages/login/login" || getCurrentRoute() == "pages/login_token/login_token"){
						me.onLoginSuccess(uni.getStorageSync("myUsername").toLowerCase());
					}
			
					let identityToken = WebIM.conn.context.accessToken
					let identityName = WebIM.conn.context.jid
			
					// service.setup({"tktId":"13H9ZH0001TE29IFLK000C7TK2","url":"wss://172.17.2.55/confr/multipeople?CONFRID=13H9ZH0001TE29IFLK000C1M3&forward=127.0.0.1&port=9092","confrId":"13H9ZH0001TE29IFLK000C1M3","password":"","type":"communication_mix","memName":"easemob-demo#chatdemoui_zdtest@easemob.com","hmac":"SSZpG1K0U6cuIl8TWWV06yXgBaQ=","timestamp":1575962109914,"rights":15}
					// )
					// service.join()
					//"{"tktId":"13H851UX8XTE10GSUIQ00C1TK2","url":"wss://rtc-turn4-hsb.easemob.com/ws?CONFRID=13H851UX8XTE10GSUIQ00C1&forward=10.29.117.29&port=9092","confrId":"13H851UX8XTE10GSUIQ00C1","password":"","type":"communication_mix","memName":"easemob-demo#chatdemoui_zdtest@easemob.com","hmac":"Ak0tITkTxIp+RFlDC/B3LKm2iMw=","timestamp":1575873756927,"rights":7}"
					// emedia.mgr.createConfr({
					// 	identityName: 'easemob-demo#chatdemoui_zdtest4@easemob.com',
					//  	identityToken: identityToken,//'YWMtLFeEbBpOEeqD-sMgAnWU5U1-S6DcShHjkNXh_7qs2vUy04pwHuER6YGUI5WOSRNCAwMAAAFu6V9A4ABPGgDCHHYPZf0jtQbrjH97smaj5nqfv0jQI3WQ2Idfa30bqg',
					//  	confrType: 11,
					// 	password: '',
					// 	success: function(data){
					// 		var ticket = JSON.parse(data.ticket)
					// 		//ticket.url = ticket.url//.replace('localhost', '172.17.2.55')
					// 		var ssss = service.setup(ticket)
					// 		console.log('ssss', ssss)
					// 		service.join()
			
					// 		uni.emedia.onAddStream=function(data){
					// 			console.log('onAddStream', data)
					// 			getApp().globalData.subUrl = data.rtmp
					// 		}
					// 	}
					// })
			
				},
				onReconnect(){
					uni.showToast({
						title: "重连中...",
						duration: 2000
					});
				},
				onSocketConnected(){
					uni.showToast({
						title: "socket连接成功",
						duration: 2000
					});
				},
				onClosed(){
					// uni.showToast({
					// 	title: "网络已断开",
					// 	icon: 'none',
					// 	duration: 2000
					// });
					uni.redirectTo({
							url: "../login/login"
						});
					getApp().globalData.closed = true;
					WebIM.conn.close();
				},
				onInviteMessage(message){
					me.globalData.saveGroupInvitedList.push(message);
					disp.fire("em.xmpp.invite.joingroup", message);
					// uni.showModal({
					// 	title: message.from + " 已邀你入群 " + message.roomid,
					// 	success(){
					// 		disp.fire("em.xmpp.invite.joingroup", message);
					// 	},
					// 	error(){
					// 		disp.fire("em.xmpp.invite.joingroup", message);
					// 	}
					// });
				},
				onReadMessage(message){
					//console.log('已读', message)
				},
				onPresence(message){
					//console.log("onPresence", message);
					switch(message.type){
					case "unsubscribe":
						// pages[0].moveFriend(message);
						break;
					// 好友邀请列表
					case "subscribe":
						if(message.status === "[resp:true]"){
			
						}
						else{
							// pages[0].handleFriendMsg(message);
							for (let i = 0; i < me.globalData.saveFriendList.length; i ++) {
						      	if(me.globalData.saveFriendList[i].from === message.from){
						      		me.globalData.saveFriendList[i] = message
						      		disp.fire("em.xmpp.subscribe");
						      		return;
						 		}
						    }
							me.globalData.saveFriendList.push(message);
							disp.fire("em.xmpp.subscribe");
						}
						break;
					case "subscribed":
						uni.showToast({
							title: "添加成功",
							duration: 1000
						});
						disp.fire("em.xmpp.subscribed");
						break;
					case "unsubscribed":
						// uni.showToast({
						// 	title: "已拒绝",
						// 	duration: 1000
						// });
						break;
					case "memberJoinPublicGroupSuccess":
						uni.showToast({
							title: "已进群",
							duration: 1000
						});
						break;
					// 好友列表
					// case "subscribed":
					// 	let newFriendList = [];
					// 	for(let i = 0; i < me.globalData.saveFriendList.length; i++){
					// 		if(me.globalData.saveFriendList[i].from != message.from){
					// 			newFriendList.push(me.globalData.saveFriendList[i]);
					// 		}
					// 	}
					// 	me.globalData.saveFriendList = newFriendList;
					// 	break;
					// 删除好友
					case "unavailable":
						disp.fire("em.xmpp.contacts.remove");
						disp.fire("em.xmpp.group.leaveGroup", message);
						break;
			
					case 'deleteGroupChat':
						disp.fire("em.xmpp.invite.deleteGroup", message);
						break;
			
					case "leaveGroup": 
						disp.fire("em.xmpp.group.leaveGroup", message);
						break;
			
					case "removedFromGroup": 
						disp.fire("em.xmpp.group.leaveGroup", message);
						break;
					// case "joinChatRoomSuccess":
					// 	uni.showToast({
					// 		title: "JoinChatRoomSuccess",
					// 	});
					// 	break;
					// case "memberJoinChatRoomSuccess":
					// 	uni.showToast({
					// 		title: "memberJoinChatRoomSuccess",
					// 	});
					// 	break;
					// case "memberLeaveChatRoomSuccess":
					// 	uni.showToast({
					// 		title: "leaveChatRoomSuccess",
					// 	});
					// 	break;
			
					default:
						break;
					}
				},
			
				onRoster(message){
					// let pages = getCurrentPages();
					// if(pages[0]){
					// 	pages[0].onShow();
					// }
				},
			
				onVideoMessage(message){
					console.log("onVideoMessage: ", message);
					if(message){
						msgStorage.saveReceiveMsg(message, msgType.VIDEO);
					}
					calcUnReadSpot(message);
					ack(message);
				},
			
				onAudioMessage(message){
					console.log("onAudioMessage", message);
					let body = {
						accessToken:message.accessToken,
						filename:message.filename,
						length:message.length,
						url:message.url
					}
					let obj = {
						self:false,
						id:msgType.AUDIO+message.id,
						type:msgType.AUDIO,
						value:message.url,
						body:body
					}
					me.$store.dispatch('pushMsg',obj)
					if(message){
						if(onMessageError(message)){
							msgStorage.saveReceiveMsg(message, msgType.AUDIO);
						}
						calcUnReadSpot(message);
						ack(message);
					}
				},
				
				onCmdMessage(message){
					console.log("onCmdMessage", message);
					if(message){
						if(onMessageError(message)){
							msgStorage.saveReceiveMsg(message, msgType.CMD);
						}
						calcUnReadSpot(message);
						ack(message);
					}
				},
			
				// onLocationMessage(message){
				// 	console.log("Location message: ", message);
				// 	if(message){
				// 		msgStorage.saveReceiveMsg(message, msgType.LOCATION);
				// 	}
				// },
			
				onTextMessage(message){
					console.log("收到文本消息", message);
					let msgData = {
						self:false,
						type:msgType.TEXT,
						value:message.data
					}
					// getMsgData(msgData,'txt')
					me.$store.dispatch('pushMsg',getMsgData(msgData,'txt'))
					// console.log(me.$store.state.msgList)
					if(message){
						if(onMessageError(message)){
							msgStorage.saveReceiveMsg(message, msgType.TEXT);
						}
						calcUnReadSpot(message);
						ack(message);
			
						if(message.ext.msg_extension){
							let msgExtension = JSON.parse(message.ext.msg_extension)
							let conferenceId = message.ext.conferenceId
							let password = message.ext.password
							disp.fire("em.xmpp.videoCall", {
								msgExtension: msgExtension,
								conferenceId: conferenceId,
								password: password
							});
						}
					}
				},
			
				onEmojiMessage(message){
					console.log("onEmojiMessage", message);
					if(message){
						if(onMessageError(message)){
							msgStorage.saveReceiveMsg(message, msgType.EMOJI);
						}
						calcUnReadSpot(message);
						ack(message);
					}
				},
			
				onPictureMessage(message){
					console.log("onPictureMessage", message);
					let msgData = {
						id:message.id,
						self:false,
						type:msgType.IMAGE,
						value:message.url
					}
					me.$store.dispatch('pushMsg',msgData)
					console.log(me.$store.state.msgList)
					return
					if(message.id){
						// if(onMessageError(message)){
						// 	msgStorage.saveReceiveMsg(message, msgType.IMAGE);
						// }
						calcUnReadSpot(message);
						ack(message);
					}
				},
			
				onFileMessage(message){
					console.log('onFileMessage', message);
					if (message) {
						if(onMessageError(message)){
							msgStorage.saveReceiveMsg(message, msgType.FILE);
						}
						calcUnReadSpot(message);
						ack(message);
					}
				},
			
				// 各种异常
				onError(error){
					console.log(error)
					// 16: server-side close the websocket connection
					if(error.type == WebIM.statusCode.WEBIM_CONNCTION_DISCONNECTED && !logout){
						if(WebIM.conn.autoReconnectNumTotal < WebIM.conn.autoReconnectNumMax){
							return;
						}
						uni.showToast({
							title: "server-side close the websocket connection",
							duration: 1000
						});
						uni.redirectTo({
							url: "../login/login"
						});
						logout = true
						return;
					}
					// 8: offline by multi login
					if(error.type == WebIM.statusCode.WEBIM_CONNCTION_SERVER_ERROR){
						uni.showToast({
							title: "offline by multi login",
							duration: 1000
						});
						uni.redirectTo({
							url: "../login/login"
						});
					}
					if(error.type ==  WebIM.statusCode.WEBIM_CONNCTION_OPEN_ERROR){
						uni.hideLoading()
						disp.fire("em.xmpp.error.passwordErr");
						// uni.showModal({
						// 	title: "用户名或密码错误",
						// 	confirmText: "OK",
						// 	showCancel: false
						// });
					}
					if (error.type == WebIM.statusCode.WEBIM_CONNCTION_AUTH_ERROR) {
						uni.hideLoading()
						disp.fire("em.xmpp.error.tokenErr");
					}
					if (error.type == 'socket_error') {///sendMsgError
						console.log('socket_errorsocket_error', error)
						uni.showToast({
							title: "网络已断开",
							icon: 'none',
							duration: 2000
						});
						disp.fire("em.xmpp.error.sendMsgErr", error);
					}
				},
			});
			this.checkIsIPhoneX();
		},
		methods:{
			onLoginSuccess: function(myName){
				uni.hideLoading()
			
				uni.redirectTo({
					url: "../chat/chat?myName=" + myName
				});
			
				// uni.redirectTo({
				// 	url: "../emediaInvite/emediaInvite?myName=" + myName
				// });
			},
			
			getUserInfo(cb){
				var me = this;
				if(this.globalData.userInfo){
					typeof cb == "function" && cb(this.globalData.userInfo);
				}
				else{
					// 调用登录接口
					uni.login({
						success(){
							uni.getUserInfo({
								success(res){
									me.globalData.userInfo = res.userInfo;
									typeof cb == "function" && cb(me.globalData.userInfo);
								}
							});
						}
					});
				}
			},
			checkIsIPhoneX: function() {
			    const me = this
			    uni.getSystemInfo({
			      	success: function (res) {
				        // 根据 model 进行判断
				        if (res.model.search('iPhone X') != -1) {
				          	me.globalData.isIPX = true
				        }
			      	}
			    })
			},
		}
	}
</script>

<style>
	/*每个页面公共css */
</style>
